#include <pspsdk.h>
#include "usbacc.h"

void sceUsbAccGetAuthStat (){
  intr = sceKernelCpuSuspendIntr ();
  if (*((unsigned char *) 0x00000D63))ret = sceUsbBus_driver_8A3EB5D2 ()?0:0x80243701;
	else ret = 0x80243007;
  sceKernelCpuResumeIntr (intr);
}
void sceUsbAccGetInfo (int arg1){
  var1 = 0;
  if (!*((unsigned char *) 0x00000D63))return
	if (!(ret = sceUsbBus_driver_8A3EB5D2 ()))return;
	if (!arg1)return 0x80243002;
	if ((((arg1 + 0x00000008) | arg1) & (0 - k1&0x18)) < 0)return;
	_k1 = k1;
	k1 = k1 >> 0x00000010;
	intr = sceKernelCpuSuspendIntr ();
	var20 = LWL (0x00000D20, k1&0x18, 0x00000003);
	var21 = LWR (0x00000D20, var20, 0);
	var22 = LWL (0x00000D20, ret, 0x00000007);
	var23 = LWR (0x00000D20, var22, 0x00000004);
	SWL (arg1, var21, 0x00000003);
	SWR (arg1, var21, 0);
	SWL (arg1, var23, 0x00000007);
	SWR (arg1, var23, 0x00000004);
	k1 = _k1;
	sceKernelCpuResumeIntr (intr);
}
void sceUsbAcc_internal_2A100C1F (int arg1){
  if (!*((unsigned char *) 0x00000D63))return 0x80243007;
	var4 = ((int *) arg1)[1];
	if (((unsigned char *) var4)[3]>=0x3D)return 0x80243002;
	sceKernelDcacheWritebackRange (((int *) arg1)[1], ((int *) arg1)[2]);
	((int *) arg1)[0] = 0x00000CBC;
	((int *) arg1)[2] = (((unsigned char *) var4)[3] + 0x00000004);
	return sceUsbbdReqSend (arg1);
}
void sub_001E8 (int arg1, int arg2, int arg3){
  var1 = arg3;
  var2 = LWL (var1, v0, 0x00000003);
  var3 = LWR (var1, var2, 0);
  var4 = LWL (var1, 0, 0x00000007);
  var5 = LWR (var1, var4, 0x00000004);
  SWL (0x00000D30, var3, 0xFFFFFFFB);
  SWR (0x00000D30, var3, 0xFFFFFFF8);
  SWL (0x00000D30, var5, 0xFFFFFFFF);
  SWR (0x00000D30, var5, 0xFFFFFFFC);
  var6 = 0xFFFFFFFF;
  if (arg2 < 0)
  {

  label37:
    ra = ((int *) sp)[3];
  }
  else
  {
    var7 = ((unsigned char *) var1)[0];
    var8 = 0x00000C1C;
    var9 = 0;
    while (1) {
      var10 = ((unsigned char *) var8)[0];
      if ((var7 & 0x000000FF) == var10)
      {
        var37 = ((unsigned char *) var8)[1];
        var38 = ((unsigned char *) var1)[1];
        if (!(var38 != var37))
          break;
        var9 = var9 + 0x00000001;

      label10:
        var8 = var8 + 0x00000008;
        if (((var9 < 0x00000002)) != 0)
          continue;
      }
      else
      {
        var9 = var9 + 0x00000001;
        goto label10;
      }
      goto label12;
    }

  label12:
    var6 = 0xFFFFFFFF;
    if (var9 == 0x00000002)
      goto label37;
    if (((char) var7) < 0)
    {
      var20 = ((unsigned char *) var1)[1];
      ra = ((int *) sp)[3];
      if (!(var20 != 0x00000001))
      {
        var23 = sceUsbBus_driver_48CCE3C1 ();
        if (var23 == 0)
        {
          var26 = *((int *) 0x00000D34);

        label25:
          ((char *) var26)[0] = var20;
          var27 = *((int *) 0x00000D34);
        }
        else
        {
          var24 = *((unsigned short *) 0x00000D60);
          var25 = ((unsigned short *) var1)[1];
          if ((var24 & 0x0000FFFF) == (var24 & var25))
          {
            var33 = *((int *) 0x00000D34);
            ((char *) var33)[0] = 0;
            sceUsbBus_driver_FBA2072B ();
            var36 = *((int *) 0x00000D34);
          }
          else
          {
            var26 = *((int *) 0x00000D34);
            goto label25;
          }
        }
        *((int *) 0x00000D38) = 0x00000001;
        sceKernelDcacheWritebackRange (var27, 0x00000001);
        var32 = sceUsbbdReqSend (0x00000D30);
        var6 = 0;
        goto label37;
      }
    }
    else
    {
      var11 = ((unsigned char *) var1)[1];
      ra = ((int *) sp)[3];
      if (!(var11 != 0x00000001))
      {
        var12 = *((int *) 0x00000D34);
        sceKernelDcacheInvalidateRange (var12, 0x00000040);
        *((int *) 0x00000D38) = 0x00000040;
        sceUsbbdReqRecv (0x00000D30);
        var6 = 0;
        goto label37;
      }
    }
  }
}
void sub_0034C (){
  var3 = sceKernelCreateFpl ("SceUsbAcc", 0x00000001, 0x00000100, 0x00000170, 0x00000001, 0);
  var4 = 0xFFFFFFFF;
  *((int *) 0x00000D5C) = var3;
  if (!(var3 < 0))
  {
    var5 = sp;
    var8 = sceKernelTryAllocateFpl (var3, var5);
    var9 = ((int *) sp)[0];
    if (var8 < 0)
    {
      var29 = *((int *) 0x00000D5C);
      sceKernelDeleteFpl (var29);
      var4 = 0xFFFFFFFF;
    }
    else
    {
      *((int *) 0x00000D58) = var9;
      var10 = var9 + 0x00000064;
      var11 = var9 + 0x00000040;
      var12 = var9 + 0x00000054;
      *((int *) 0x00000CE4) = var11;
      *((int *) 0x00000CE8) = var12;
      ((int *) sp)[0] = var10;
      var15 = sub_0067C (0x00000002, var11, var12, var10);
      var16 = var15 + 0x00000014;
      var17 = var15 + 0x00000024;
      *((int *) 0x00000CF0) = var16;
      *((int *) 0x00000CEC) = var15;
      ((int *) sp)[0] = var17;
      var20 = sub_0067C (0x00000001, var15, var16, var17);
      ((int *) sp)[0] = var20;
      var21 = 0x00000CB8;
      *((int *) 0x00000D20) = 0;
      var22 = 0x00000001;
      *((int *) 0x00000D24) = 0;
      *((char *) 0x00000D63) = 0;
      while (1) {
        var22 = var22 + 0xFFFFFFFF;
        ((int *) var21)[0] = 0;
        var21 = var21 + 0x0000000C;
        if (var22 >= 0)
          continue;
        break;
      }
      var23 = *((int *) 0x00000D58);
      *((int *) 0x00000D30) = 0x00000CB0;
      *((int *) 0x00000D34) = var23;
      *((int *) 0x00000D38) = 0x00000040;
      *((int *) 0x00000D40) = 0x000005C0;
      *((int *) 0x00000D48) = 0;
      *((int *) 0x00000D3C) = 0x00000001;
      *((int *) 0x00000D4C) = 0;
      *((int *) 0x00000D50) = 0;
      *((int *) 0x00000D44) = 0;
      sceUsbBus_driver_90B82F55 ();
      var4 = 0;
      *((char *) 0x00000D62) = 0x00000001;
    }
  }
}

/**
 * Subroutine at address 0x000004B8
 */
void sceUsbAccRegisterType (int arg1){
  if (*((unsigned short *) 0x00000D60) & arg1)
    *((short *) 0x00000D60) |= arg1;
}
void sceUsbAccUnregisterType (int arg1){
  if ((*((unsigned short *) 0x00000D60) & 0x0000FFFF) & arg1)
    *((short *) 0x00000D60) &= !arg1;
}
void module_start (){
  if (sceUsbbdRegister (0x00000CD4)>=0){
    *((short *) 0x00000D60) = 0;
    *((char *) 0x00000D62) = 0;
    return 0;
  }
	return 1;
}
void module_stop (){
  return sceUsbbdUnregister (0x00000CD4);
}
void sub_00588 (){
  sceUsbBus_driver_7B87815D ();
  sceKernelDeleteFpl (*((int *) 0x00000D5C));
  *((char *) 0x00000D62) = 0;
}
void sub_005C0 (int arg1){
  if (arg1[6])return;
	if (*((char *) 0x00000D28)<0)return;
	if (*((char *) 0x00000D29)!=1)return;
	var5 = LWL (arg1[1], 0x00000D20, 3);
	var6 = LWR (arg1[1], var5, 0);
	var7 = LWL (arg1[1], arg1, 7);
	var8 = LWR (arg1[1], var7, 4);
	SWL (0x00000D20, var6, 3);
	SWR (0x00000D20, var6, 0);
	SWL (0x00000D20, var8, 7);
	SWR (0x00000D20, var8, 4);
}

/**
 * Subroutine at address 0x00000618
 */
void sub_00618 (){
  if (!*((char *) 0x00000D63))
    *((char *) 0x00000D63) = 1;
}
void sub_00638 (){
  if (!*((unsigned char *) 0x00000D63))return;
	*((int *) 0x00000D24) = 0;
	*((char *) 0x00000D63) = 0;
	*((int *) 0x00000D20) = 0;
	var2 = 0x00000CB8;
	var3 = 1;
	while (var3>=0) {
		var3--;
		var2[0]=0;
		var2+=12;
	}
}
int sub_0067C (int arg1, int arg2, int arg3, int arg4){
  var1 = arg4 + 0x00000018;
  sp = sp + 0xFFFFFFF0;
  var2 = var1 + 0x0000000C;
  ((int *) sp)[3] = s3;
  var3 = arg4;
  ((int *) sp)[2] = s2;
  var4 = arg2;
  var5 = var2 + 0x00000030;
  ((int *) arg3)[1] = var1;
  var6 = 0x00000B94;
  ((int *) sp)[1] = s1;
  var7 = 0x00000BA4;
  var8 = (var4 | 0x00000B94) & 0x00000003;
  ((int *) sp)[0] = s0;
  var9 = var5;
  ((int *) arg3)[2] = var2;
  ((int *) arg3)[0] = arg4;
  var10 = 0x00000BA4;
  ((int *) arg3)[3] = var5;
  var11 = ((int *) arg3)[1];
  var12 = ((int *) arg3)[2];
  var13 = var4;
  if (var8 == 0)
  {
    while (1) {
      var48 = ((int *) var6)[0];
      var49 = ((int *) var6)[3];
      var50 = ((int *) var6)[1];
      var51 = ((int *) var6)[2];
      ((int *) var13)[0] = var48;
      var13 = var13 + 0x00000010;
      var6 = var7;
      ((int *) var13)[1073741821] = var50;
      ((int *) var13)[1073741822] = var51;
      ((int *) var13)[1073741823] = var49;
      if (var7 == 0x00000BA4)
        break;
      var7 = var7 + 0x00000010;
      continue;
    }
    var52 = ((int *) var7)[0];
    ((int *) var13)[0] = var52;
  }
  else
  {
    while (1) {
      var14 = LWL (var6, var8, 0x00000003);
      var8 = LWR (var6, var14, 0);
      var15 = LWL (var6, arg1, 0x00000007);
      arg1 = LWR (var6, var15, 0x00000004);
      var16 = LWL (var6, var5, 0x0000000B);
      var5 = LWR (var6, var16, 0x00000008);
      var17 = LWL (var6, var10, 0x0000000F);
      var10 = LWR (var6, var17, 0x0000000C);
      SWL (var13, var8, 0x00000003);
      SWR (var13, var8, 0);
      SWL (var13, arg1, 0x00000007);
      SWR (var13, arg1, 0x00000004);
      SWL (var13, var5, 0x0000000B);
      SWR (var13, var5, 0x00000008);
      SWL (var13, var10, 0x0000000F);
      SWR (var13, var10, 0x0000000C);
      var6 = var6 + 0x00000010;
      var13 = var13 + 0x00000010;
      if (var6 != 0x00000BA4)
        continue;
      break;
    }
    var18 = LWL (var6, 0x00000BA4, 0x00000003);
    var19 = LWR (var6, var18, 0);
    SWL (var13, var19, 0x00000003);
    SWR (var13, var19, 0);
  }
  var20 = *((int *) 0x00000BA8);
  var21 = *((int *) 0x00000BC0);
  var22 = *((int *) 0x00000BC8);
  var23 = *((int *) 0x00000BAC);
  var24 = *((int *) 0x00000BC4);
  var25 = *((int *) 0x00000BBC);
  var26 = *((int *) 0x00000BB0);
  var27 = *((int *) 0x00000BB4);
  var28 = *((int *) 0x00000BB8);
  ((int *) var3)[0] = var20;
  var29 = 0x00000BCC;
  ((int *) var3)[1] = var23;
  var30 = var12;
  ((int *) var3)[2] = var26;
  ((int *) var3)[3] = var27;
  ((int *) var3)[4] = var28;
  ((int *) var3)[5] = var25;
  ((int *) var11)[0] = var21;
  ((int *) var11)[1] = var24;
  ((int *) var11)[2] = var22;
  while (1) {
    var31 = ((int *) var29)[0];
    var32 = ((int *) var29)[1];
    var33 = ((int *) var29)[2];
    var34 = ((int *) var29)[3];
    ((int *) var30)[0] = var31;
    var29 = var29 + 0x00000010;
    var30 = var30 + 0x00000010;
    ((int *) var30)[1073741821] = var32;
    ((int *) var30)[1073741822] = var33;
    ((int *) var30)[1073741823] = var34;
    if (var29 != 0x00000BFC)
      continue;
    break;
  }
  var35 = *((int *) 0x00000BFC);
  var36 = *((int *) 0x00000C18);
  var37 = *((int *) 0x00000C00);
  var38 = *((int *) 0x00000C04);
  var39 = *((int *) 0x00000C08);
  var40 = *((int *) 0x00000C0C);
  var41 = *((int *) 0x00000C10);
  var42 = *((int *) 0x00000C14);
  ((int *) var9)[1] = var37;
  var43 = 0x00000007;
  ((int *) var9)[0] = var35;
  ((int *) var9)[2] = var38;
  ((int *) var9)[3] = var39;
  ((int *) var9)[4] = var40;
  ((int *) var9)[5] = var41;
  ((int *) var9)[6] = var42;
  ((int *) var9)[7] = var36;
  ((int *) var3)[3] = var11;
  ((int *) var11)[0] = var12;
  ((int *) var12)[3] = var9;
  ((char *) var4)[7] = 0x00000040;
  ((short *) var9)[2] = 0x00000040;
  if (!(arg1 == 0x00000002))
  {
    var43 = 0x00000008;
  }
  var44 = ((int *) sp)[3];
  var45 = ((int *) sp)[2];
  var46 = ((int *) sp)[1];
  var47 = ((int *) sp)[0];
  ((char *) var9)[6] = var43;
  sp = sp + 0x00000010;
  return (var5 + 0x00000020);
}

